﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using UIWindows.Models.Devices;
using XingLucifer.Devices;
using XingLucifer.IBase;

namespace UIWindows.Tools.Converter
{
    public class DeviceClientModelConverter : JsonConverter<DeviceClientModel>
    {
        public override DeviceClientModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            DeviceClientModel deviceClientModel = new DeviceClientModel();
            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    string propertyName = reader.GetString();
                    reader.Read();
                    if (propertyName == "PLC")
                    {
                        // Skip the PLC property to keep it null
                        //reader.Skip();
                        //while (reader.TokenType != JsonTokenType.EndObject && reader.TokenType != JsonTokenType.EndArray)
                        //{
                        //    reader.Read();
                        //}
                        typeof(DeviceClientModel).GetProperty(propertyName)?.SetValue(deviceClientModel, null);
                    }
                    else
                    {
                        // Use default deserialization for other properties
                        //object value = JsonSerializer.Deserialize(ref reader, typeof(DeviceClientModel).GetProperty(propertyName).PropertyType, options);
                        //typeof(DeviceClientModel).GetProperty(propertyName)?.SetValue(deviceClientModel, value);

                        PropertyInfo propertyInfo = typeof(DeviceClientModel).GetProperty(propertyName);
                        if (propertyInfo != null)
                        {
                            object value = JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options);
                            propertyInfo.SetValue(deviceClientModel, value);
                        }
                        else
                        {
                            reader.Skip();
                            while (reader.TokenType != JsonTokenType.EndObject && reader.TokenType != JsonTokenType.EndArray)
                            {
                                reader.Read();
                            }
                        }
                    }
                }
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return deviceClientModel;
                }
            }
            throw new JsonException();
        }

        public override void Write(Utf8JsonWriter writer, DeviceClientModel value, JsonSerializerOptions options)
        {
            throw new NotImplementedException();
        }
    }
    //public class DeviceClientModelConverter : JsonConverter<DeviceClientModel>
    //{
    //    public override DeviceClientModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    //    {
    //        DeviceClientModel deviceClientModel = new DeviceClientModel();
    //        while (reader.Read())
    //        {
    //            if (reader.TokenType == JsonTokenType.PropertyName)
    //            {
    //                string propertyName = reader.GetString();
    //                reader.Read();
    //                if (propertyName == "PLC")
    //                {
    //                    // Skip the PLC property to keep it null
    //                    reader.Skip();
    //                }
    //                else
    //                {
    //                    // Use default deserialization for other properties
    //                    object value = JsonSerializer.Deserialize(ref reader, typeof(DeviceClientModel).GetProperty(propertyName).PropertyType, options);
    //                    typeof(DeviceClientModel).GetProperty(propertyName)?.SetValue(deviceClientModel, value);
    //                }
    //            }
    //            if (reader.TokenType == JsonTokenType.EndObject)
    //            {
    //                return deviceClientModel;
    //            }
    //        }
    //        throw new JsonException();
    //    }

    //    public override void Write(Utf8JsonWriter writer, DeviceClientModel value, JsonSerializerOptions options)
    //    {
    //        throw new NotImplementedException();
    //    }
    //}
    //public class DeviceClientModelConverter : JsonConverter<DeviceClientModel>
    //{
    //    public override DeviceClientModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    //    {
    //        DeviceClientModel deviceClientModel = new DeviceClientModel();
    //        while (reader.Read())
    //        {
    //            if (reader.TokenType == JsonTokenType.PropertyName)
    //            {
    //                string propertyName = reader.GetString();
    //                reader.Read();
    //                if (propertyName == "PLC")
    //                {
    //                    // Skip the PLC property to keep it null
    //                    reader.Skip();
    //                }
    //                else
    //                {
    //                    // Use default deserialization for other properties
    //                    typeof(DeviceClientModel).GetProperty(propertyName)?.SetValue(deviceClientModel, JsonSerializer.Deserialize(ref reader, typeof(DeviceClientModel).GetProperty(propertyName).PropertyType));
    //                }
    //            }
    //            if (reader.TokenType == JsonTokenType.EndObject)
    //            {
    //                return deviceClientModel;
    //            }
    //        }
    //        throw new JsonException();
    //    }

    //    public override void Write(Utf8JsonWriter writer, DeviceClientModel value, JsonSerializerOptions options)
    //    {
    //        throw new NotImplementedException();
    //    }
    //}
}
